﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SweetSurvey.Controllers;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;
using SweetSurvey.Models.ViewModels;

namespace SweetSurvey.Tests.ControllerTests
{
    /// <summary>
    ///This is a test class for SurveyControllerTest and is intended
    ///to contain all SurveyControllerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SurveyControllerTest : ControllerTestBase
    {
        /// <summary>
        ///A test for FillOut
        ///</summary>
        [TestMethod]
        public void FillOutTest()
        {
            // variables
            var surveyId = "fakeId";
            var surveyTitle = "fakeTitle";

            // prepare
            var mockSurveyRepository = new Mock<ISurveyRepository>(MockBehavior.Strict);
            mockSurveyRepository.Setup(r => r.Get(surveyId, true)).Returns(new Survey() { Title = surveyTitle });
            SurveyController target = new SurveyController(null, mockSurveyRepository.Object, null, null, null);
            
            // act
            var actionResult = target.FillOut(surveyId);

            // assert
            mockSurveyRepository.VerifyAll();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            Assert.AreEqual<string>(target.ViewBag.SurveyId, surveyId);
            Assert.AreEqual<string>(target.ViewBag.Title, surveyTitle);
        }

        /// <summary>
        ///A test for Submitted
        ///</summary>
        [TestMethod]
        public void SubmittedTest()
        {
            // variables
            var surveyId = "fakeId";
            var surveyTitle = "fakeTitle";
            var answerCount = 2;
            var optionCount = 10;
            var answers = new List<int>();
            var options = new List<Option>();
            for (var i = 0; i < optionCount; i++) { answers.Add(answerCount); options.Add(new Option()); }
            var questionCount = 5;
            var questions = new List<Question>();
            for (var i = 0; i < questionCount; i++) { questions.Add(new Question()); }

            // prepare
            var mockSurveyRepository = new Mock<ISurveyRepository>(MockBehavior.Strict);
            mockSurveyRepository.Setup(s => s.Get(surveyId, true))
                .Returns(new Survey() { Title = surveyTitle });
            var mockQuestionRepository = new Mock<IQuestionRepository>(MockBehavior.Strict);
            mockQuestionRepository.Setup(q => q.GetQuestionsBySurveyId(surveyId))
                .Returns(questions.AsQueryable<Question>());
            var mockAnswerRepository = new Mock<IAnswerRepository>(MockBehavior.Strict);
            mockAnswerRepository.Setup(a => a.GetAnswerCountsByQuestionId(It.IsAny<string>()))
                .Returns(answers);
            var mockOptionRepository = new Mock<IOptionRepository>(MockBehavior.Strict);
            mockOptionRepository.Setup(o => o.GetOptionsByQuestionId(It.IsAny<string>()))
                .Returns(options.AsQueryable<Option>());
            
            SurveyController target = new SurveyController(
                null, mockSurveyRepository.Object, mockQuestionRepository.Object, mockOptionRepository.Object, mockAnswerRepository.Object);

            // act
            var actionResult = target.Submitted(surveyId);

            // assert
            mockSurveyRepository.VerifyAll();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            Assert.IsInstanceOfType(((ViewResult)actionResult).Model, typeof(ResultsViewModel));
            var array = (ResultsViewModel)(((ViewResult)actionResult).Model);
            foreach (var item in array)
            {
                Assert.AreEqual<int>(item.Values.Count, optionCount);
                foreach (var c in item.Values) { Assert.AreEqual<int>(c, answerCount); }
            }
            Assert.AreEqual<int>(array.Count, questionCount);
        }

        /// <summary>
        ///A test for Published
        ///</summary>
        [TestMethod]
        public void PublishedTest()
        {
            // variables
            var surveyId = "fakeId";

            // prepare
            var target = new SurveyController(null, null, null, null, null);

            // act
            var actionResult = target.Published(surveyId);

            // assert
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            Assert.AreEqual<string>(target.ViewBag.SurveyId, surveyId);
        }

        /// <summary>
        ///A test for Publish
        ///</summary>
        [TestMethod]
        public void PublishTest()
        {
            // variables
            var surveyId = "fakeSurveyId";
            var title = "fakeTitle";
            var questionId = "fakeQuestionId";
            ICollection<QuestionViewModel> questions = new Collection<QuestionViewModel>();
            var questionCount = 10;
            for (var i = 0; i < questionCount; i++)
            {
                questions.Add(new QuestionViewModel(new Question() { Id = surveyId, Title = title }));
            }
            var questionsString = (new JavaScriptSerializer()).Serialize(questions);
            var url = "fakeUrl";

            var mockEditorRepository = new Mock<IEditorRepository>();
            var mockSurveyRepository = new Mock<ISurveyRepository>();
            mockSurveyRepository.Setup(s => s.Create(It.IsAny<Survey>())).Returns(surveyId);
            mockSurveyRepository.Setup(s => s.Get(surveyId, true)).Returns(new Survey());
            var mockQuestionRepository = new Mock<IQuestionRepository>();
            mockQuestionRepository.Setup(q => q.DeleteQuestionsBySurveyId(surveyId));
            mockQuestionRepository.Setup(q => q.Create(It.IsAny<Question>())).Returns(questionId);
            var mockOptionRepository = new Mock<IOptionRepository>(MockBehavior.Strict);
            mockOptionRepository.Setup(o => o.CreateOptionsByQuestionId(It.IsAny<string>(), It.IsAny<IList<string>>()));
            var mockUrlHelper = new Mock<IUrlHelper>();
            mockUrlHelper.Setup(u => u.Action(It.IsAny<string>(), It.IsAny<object>())).Returns(url);

            SurveyController target = new SurveyController(
                mockEditorRepository.Object, mockSurveyRepository.Object, mockQuestionRepository.Object, mockOptionRepository.Object, null);
            target.ControllerContext = this.GetControllerContext(target);
            target.Url = mockUrlHelper.Object;

            // act
            var actionResult = target.Publish(surveyId, title, questionsString);

            // assert
            Assert.IsInstanceOfType(actionResult, typeof(ContentResult));
            Assert.AreEqual<string>(((ContentResult)actionResult).Content, url);
        }

        /// <summary>
        ///A test for EditSurvey
        ///</summary>
        [TestMethod]
        public void EditSurveyTest()
        {
            // variables
            var surveyId = "fakeId";
            var surveyTitle = "fakeTitle";
            var url = "fakeUrl";

            // prepare
            var mockSurveyRepository = new Mock<ISurveyRepository>(MockBehavior.Strict);
            mockSurveyRepository.Setup(s => s.Get(surveyId, true))
                .Returns(new Survey() { Title = surveyTitle });
            var mockUrlHelper = new Mock<IUrlHelper>();
            SurveyController target = new SurveyController(
                null, mockSurveyRepository.Object, null, null, null);
            mockUrlHelper.Setup(u => u.Action(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>()))
                .Returns(url);
            target.Url = mockUrlHelper.Object;

            // act
            var actionResult = target.EditSurvey(surveyId);

            // assert
            mockSurveyRepository.VerifyAll();
            mockUrlHelper.VerifyAll();
            Assert.IsInstanceOfType(actionResult, typeof(PartialViewResult));
            Assert.AreEqual<string>(target.ViewBag.SurveyId, surveyId);
            Assert.AreEqual<string>(target.ViewBag.QuestionsAjaxUrl, url);
            Assert.AreEqual<string>(target.ViewBag.SurveyTitle, surveyTitle);
        }
    }
}
